package com.luhui.codereview

import com.intellij.openapi.components.ProjectComponent
import com.intellij.openapi.fileEditor.FileEditorManagerListener
import com.intellij.openapi.project.Project
import com.luhui.codereview.listeners.FileListener
import com.intellij.ui.content.ContentFactory
import com.intellij.openapi.wm.ToolWindowAnchor
import com.intellij.openapi.wm.ToolWindowManager
import com.luhui.codereview.service.ProjectService
import com.luhui.codereview.swing.CodeReviewPanel
import com.luhui.codereview.token.LocalUserInfo
import com.luhui.codereview.token.LocalUserManager


/**
 * @author  luhui
 * @date  2019/10/24 23:46
 * @version 1.0
 */
class CodeReviewProjectComponent (val project:Project) : ProjectComponent {


    override fun projectOpened() {
        load()

        //加载项目信息
        ProjectService.getInstance(project).saveAndGetProjectInfo()

        //加载用户信息
        val localUserInfo = LocalUserManager.initUserInfo(project)
        val codeReviewPanel =  CodeReviewPanel.getInstance(project)
        codeReviewPanel.selfUserTab.emailText.text = localUserInfo?.email
        codeReviewPanel.selfUserTab.realNameText.text = localUserInfo?.realName

        //注册文件打开事件
        val messageBus = project.messageBus
        messageBus.connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, FileListener(project))


        val toolWindowManager = ToolWindowManager.getInstance(this.project)
        val toolWindow = toolWindowManager.registerToolWindow("code review", false, ToolWindowAnchor.BOTTOM)
        val contentFactory = ContentFactory.SERVICE.getInstance()
        val content = contentFactory.createContent(CodeReviewPanel.getInstance(project), null, false)




        //给控制台添加code review Tab
        toolWindow.contentManager.addContent(content)




    }


}